package io.renren.service.impl;

import io.renren.common.utils.R;
import io.renren.dao.VipCardDao;
import io.renren.entity.UserEntity;
import io.renren.entity.VipCardEntity;
import io.renren.service.UserService;
import io.renren.service.VipCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class VipCardServiceImpl implements VipCardService {

    @Autowired
    VipCardDao vipCardDao;
@Autowired
    UserService userService;
    @Override
    public R setVip(Map<String, Object> params) {
        String msg="";
        Integer code=0;
        VipCardEntity vipCardEntity=vipCardDao.getCardByPassword(params);
        UserEntity userEntity=userService.queryByUserId(Long.parseLong(((Integer) params.get("userId")).toString()));
        if(userEntity.getLevel()==1){
            return  R.error(101,"您已经是vip了");
        }
        if(vipCardEntity==null){
            return   R.error(101,"卡密输入错误");
        }
        else  if(vipCardEntity.getIsUsed()==1){
            return   R.error(101,"此卡已使用");
        }
        else {
            vipCardEntity.setIsUsed(1);
            vipCardEntity.setUsername(userEntity.getUsername());
            vipCardEntity.setMobile(userEntity.getMobile());
            vipCardDao.userVipCard(vipCardEntity);

            userEntity.setLevel(1);
            userService.updateById(userEntity);
            return R.ok("绑定成功",0);
        }
        //return R.ok();
    }
}
